/*
-----------------------------------------------------------------------------
Filename:    Ship.cpp

-----------------------------------------------------------------------------
*/

#include "Enemy.h"

//-------------------------------------------------------------------------------------

Enemy::Enemy(Ogre::SceneManager* sceneMgr, MOC::CollisionTools* mCollisionTools, int index){
	
	// Scene manager interno
	mSceneMgr = sceneMgr;
	mMOC = mCollisionTools;

	// models
	loadModel(index);
	// particles
	setupParticles(index);

	// enemy Variables
	enemySpeed = 5.0;
	life = 100.0;
	isAlive = true;

}

// cargar modelos
void Enemy::loadModel(int i){	
	
	// Enemy model
	enemyEntity = mSceneMgr->createEntity( "Zombie2.mesh");
	enemyEntity->setCastShadows(true);
	enemyEntity->setQueryFlags(ENEMY_MASK);

	// for node
	char aux[20];
	sprintf(aux, "E.%d", i);

	enemyNode = mSceneMgr->getRootSceneNode()->createChildSceneNode( Ogre::String(aux) );
		// position of ship based on bounding box
	Ogre::AxisAlignedBox box = enemyEntity->getBoundingBox();
	enemyNode->setPosition( 0.0, -box.getCorner(Ogre::AxisAlignedBox::FAR_LEFT_BOTTOM).y + 20.0, 0.0);
	enemyNode->attachObject(enemyEntity);
	enemyNode->scale(1.5, 1.5, 1.5);

	// ---------------------------------------------------------------------------------------------------
	
	// animaciones
	//Animations
	anim_walk = enemyEntity->getAnimationState("Andar");	
	anim_walk->setEnabled(true);
	anim_walk->setLoop(true);

	anim_shoot = enemyEntity->getAnimationState("Atacar");
	anim_shoot->setEnabled(false);
	anim_shoot->setLoop(true);

	anim_dead = enemyEntity->getAnimationState("Muerte");
	anim_dead->setEnabled(false);
	anim_dead->setLoop(false);	

}

// inicializar particulas
void Enemy::setupParticles(int i){

	// particle system
	
	char aux2[20];
	sprintf(aux2, "TurretSmoke-%d", i);

	smokeParticles = mSceneMgr->createParticleSystem(Ogre::String(aux2), "TurretSmoke");
	smokeParticles->setEmitting(false);

	enemyNode->attachObject(smokeParticles);

}

// -----------------------------------------------------------------------------
// procesar animaciones
void Enemy::processAnimations(const Ogre::FrameEvent& evt){

	// Idle
	anim_walk->addTime(evt.timeSinceLastFrame);

	
}

// ----------------------------------------------------------------------------------
// move the ship
void Enemy::moveEnemy(const Ogre::FrameEvent& evt){
		
	if(isAlive){
		// ALTURA DEL TERRENO
		float nuevaY = mMOC->getCalculateY(enemyNode, RACETRACK_MASK) ;
		enemyNode->setPosition(enemyNode->getPosition().x, nuevaY + 0.1, enemyNode->getPosition().z);

		// moving the enemy
		enemyNode->yaw( Ogre::Degree(0.3) );
		enemyNode->translate( enemySpeed * evt.timeSinceLastFrame ,0.0 , 0.0, Ogre::Node::TS_LOCAL);

		// animations
		processAnimations(evt);
	}else{
		// controlar animacion muerte
		if ( anim_dead->getTimePosition() < 1.14 )
			anim_dead->addTime(evt.timeSinceLastFrame);
	}
}

//-------------------------------------------------------------------------------------
// damage the enemy
void Enemy::damage(float damage){

	life -= damage;

	// Dead
	if (life < 0){
		smokeParticles->setEmitting(true);
		anim_dead->setEnabled(true);
		anim_walk->setEnabled(false);
		isAlive = false;
	}

}